#!/bin/bash

source /home/shell/util.sh

if [ ! -f "/usr/sbin/exportfs" ]; then
    if [ "$(ostype)" == "Ubuntu" ]; then
        apt-get install -y nfs-kernel-server rpcbind
    else
        yum install -y nfs-utils rpcbind
    fi
fi
serversPrint
read -p "你的IP[默认 *]：" remote_host
remote_host=${remote_host:-"*"};

read -p "请输入挂载目录[默认/]：" local_dir
local_dir=${local_dir:-"/"};


systemctl enable --now rpcbind
systemctl enable --now nfs-server

# firewall-addservice nfs
# firewall-addservice nfs3
# firewall-addservice rpc-bind
# firewall-addservice mountd

firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='$remote_host' accept"
firewall-cmd --reload

if [ ! -d "${local_dir}" ]; then
    mkdir -p ${local_dir}
fi
# all_squash 不管你访问共享目录的用户是谁，都必须压缩为nfsnobody用户的权限
# no_root_squash 访问共享目录时，用户如果是root权限，对共享目录也具有root权限
# root_squash 如果访问共享目录是root的权限用户，对共享目录的权限会被压缩为nfsnobody用户的权
tee /etc/exports -a<<EOF
${local_dir} ${remote_host}/24(rw,sync,no_subtree_check,no_root_squash)
EOF

exportfs -rv
/etc/init.d/rpcbind restart
/etc/init.d/nfs-kernel-server restart

printInfo "=======================nfs======================="
printInfo "nfs windows访问：\\\\\\${host_addr}${local_dir////\\}"
printInfo "mount访问：mount -t nfs ${host_addr}:${local_dir} /localdir"
printInfo "=======================nfs======================="